<template>
  <div>
    <van-nav-bar
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <div class="content">
      <van-form @submit="onSubmit">
        <van-field
          v-model="userinfo.userName"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
          v-model="userinfo.passWord"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
        <div style="margin: 16px;">
          <van-button round block type="info" native-type="submit"
            >登录</van-button
          >
          <p class="register" @click="$router.push('/register')">无账号,请注册</p>
        </div>
      </van-form>
    </div>
  </div>
</template>

<script>
import { login } from '@api/home'
import { Toast } from 'vant'
export default {
  name: 'login',
  data () {
    return {
      userinfo: {
        userName: '',
        passWord: ''
      }
    }
  },
  methods: {
    onClickLeft () {
      this.$router.push('/home')
    },
    onSubmit () {
      window.localStorage.clear()
      this.$store.commit('Order/change_contact', {})
      login(this.userinfo).then((res) => {
        if (res.data.status !== 200) {
          this.$toast.fail('登录失败')
        } else {
          window.localStorage.setItem('auth_token', res.data.token)
          var a = this.$route.query.from
          if (a) {
            this.$router.push(`${a}`)
            console.log(this.$router.push(`${a}`))
          } else {
            this.$router.push('/home')
          }
          this.$toast.success('登录成功')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.content {
  margin-top: 20px;
}
.register{
  margin-top: 20px;
  text-align: right;
  color:blue;
  font-size: 13px;
}
</style>
